home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue51 / Observer / Subject_Portfolio.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1999-10-08  |  5.3 KB  |  180 lines

  1. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2.   (c) TechInsite Pty. Ltd.
  3.   PO Box 429, Abbotsford, Melbourne. 3067 Australia
  4.   Phone: +61 3 9419 6456
  5.   Fax:   +61 3 9419 1682
  6.   Web:   www.techinsite.com.au
  7.   EMail: peter_hinrichsen@techinsite.com.au
  8.  
  9.   Created: October 1999
  10.  
  11.   Notes: Our 'Subject' class.
  12.          We are modeling a portfolio of shares, so the main
  13.          object (TPortfolio) is basically a TList of
  14.          TStockTrans objects, which contain data about a
  15.          transaction we made. Data in TStockTrans includes
  16.          the share's name, company name, quantity and price.
  17.  
  18. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  19. unit Subject_Portfolio;
  20.  
  21. interface
  22. uses
  23.    Classes
  24.   ,FObserver_Abstract
  25.   ;
  26.  
  27. type
  28.  
  29.   // TPortfolio, a holder of TStockTrans objects, with an
  30.   // additional method to calculate the total
  31.   // portfolio value.
  32.   //--------------------------------------------------------
  33.   TPortfolio = class( TSubjectAbstract )
  34.   private
  35.     // A TList to hold the TStockTrans objects
  36.     FStocks : TList ;
  37.   public
  38.     constructor create ;
  39.     destructor  destroy ; override ;
  40.     property    stocks : TList read FStocks ;
  41.     // Load some test data into the object
  42.     procedure   LoadData ;
  43.     // Calculate the total value of the portfolio
  44.     function    Total : real ;
  45.   end;
  46.  
  47.   // A holder for data about a share:
  48.   // StockCode, company name, quantity and price
  49.   //--------------------------------------------------------
  50.   TStockTrans = class( TObject )
  51.   private
  52.     FiQty: integer;
  53.     FsStockCode: string;
  54.     FsStockName: string;
  55.     FrPrice: real;
  56.     function GetValue: real;
  57.    public
  58.     constructor createExt( const pStockCode : string ;
  59.                            const pStockName : string ;
  60.                            const pQty : integer      ;
  61.                            const pPrice : real )     ;
  62.  
  63.     property StockCode : string read FsStockCode write FsStockCode ;
  64.     property StockName : string read FsStockName write FsStockName ;
  65.     property Price : real read FrPrice write FrPrice ;
  66.     property Qty : integer read FiQty write FiQty ;
  67.     property Value : real read GetValue ;
  68.   end ;
  69.  
  70. // A function pointing to our single instance of the
  71. // TPortfolio class
  72. function gPortfolio : TPortfolio ;
  73.  
  74. implementation
  75. uses
  76.   SysUtils
  77.   ;
  78.  
  79. // Our single instance of the TPortfolio class
  80. var
  81.   uPortfolio : TPortfolio ;
  82.  
  83. // A 'poor man's singleton' (See the FactoryPattern article)
  84. //----------------------------------------------------------
  85. function gPortfolio : TPortfolio ;
  86. begin
  87.   if uPortfolio = nil then begin
  88.     uPortfolio := TPortfolio.Create;
  89.     uPortfolio.LoadData ;
  90.   end ;
  91.   result := uPortfolio ;
  92. end ;
  93.  
  94. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  95. // *
  96. // * TPortfolio
  97. // *
  98. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  99. constructor TPortfolio.create;
  100. begin
  101.   inherited ;
  102.   FStocks := TList.Create ;
  103. end;
  104.  
  105. //----------------------------------------------------------
  106. destructor TPortfolio.destroy;
  107. var
  108.   i : integer ;
  109. begin
  110.   // Destroy all the TStockTrans objects owned by the
  111.   // TList, FStocks
  112.   for i := 0 to FStocks.Count - 1 do
  113.     TObject( FStocks.Items[i] ).Free ;
  114.   FStocks.Free ;
  115.   inherited ;
  116. end;
  117.  
  118. //----------------------------------------------------------
  119. procedure TPortfolio.LoadData;
  120. begin
  121.   Stocks.Add( TStockTrans.CreateExt( 'BHP', 'Broken Hill Prop',
  122.                                              500, 17.50 )) ;
  123.   Stocks.Add( TStockTrans.CreateExt( 'AMC', 'Amcor',
  124.                                              700,  7.20 )) ;
  125.   Stocks.Add( TStockTrans.CreateExt( 'CML', 'Coles Myer',
  126.                                              500,  6.90 )) ;
  127.   Stocks.Add( TStockTrans.CreateExt( 'NAB', 'National Bank',
  128.                                              200, 22.00 )) ;
  129.   Stocks.Add( TStockTrans.CreateExt( 'TLS', 'Telstra',
  130.                                              400,  7.50 )) ;
  131. end;
  132.  
  133. // Calculate the value of the portfolio (sum all the values
  134. // of the stocks in the portfolio)
  135. //----------------------------------------------------------
  136. function TPortfolio.Total: real;
  137. var
  138.   i : integer ;
  139. begin
  140.   result := 0 ;
  141.   for i := 0 to Stocks.count - 1 do
  142.     result := result + ( TObject( Stocks.Items[i] ) As
  143.                                        TStockTrans ).Value ;
  144. end;
  145.  
  146. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  147. // *
  148. // *  TStockTrans
  149. // *
  150. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  151. // An extended constructor (hence the name CreateExt) to
  152. // simplify the creation of TStockTrans instances
  153. constructor TStockTrans.createExt( const pStockCode : string ;
  154.                                    const pStockName : string ;
  155.                                    const pQty : integer ;
  156.                                    const pPrice : real ) ;
  157. begin
  158.   Create ;
  159.   StockCode := pStockCode ;
  160.   StockName := pStockName ;
  161.   Qty       := pQty       ;
  162.   Price     := pPrice ;
  163. end ;
  164.  
  165. // Calculate the value of a given stock holding
  166. //----------------------------------------------------------
  167. function TStockTrans.GetValue: real;
  168. begin
  169.   result := Price * Qty ;
  170. end;
  171.  
  172. initialization
  173.   // Create an instance of the Subject (portfolio object)
  174.   gPortfolio ;
  175.  
  176. finalization
  177.   uPortfolio.free;
  178.  
  179. end.
  180.